package servlet;

import bean.Course;
import bean.User;
import dao.CourseDao;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ServletAddCourse extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Course course = new Course();
        String info = req.getParameter("info");
        String week = req.getParameter("week");
        int lessonStart = Integer.parseInt(req.getParameter("lessonStart"));
        int lessonEnd = Integer.parseInt(req.getParameter("lessonEnd"));

        String day_of_week = req.getParameter("day_of_week");
        String date = req.getParameter("date");
        String teacher = req.getParameter("teacher");
        String place = req.getParameter("place");

        course.setCourseInfo(info);
        course.setWeek(week);
        course.setLessonStart(lessonStart);
        course.setLessonEnd(lessonEnd);

        course.setDay_of_week(day_of_week);
        course.setDate(date);
        course.setTeacher(teacher);
        course.setPlace(place);

        System.out.println("这里是添加：" + course);
        // 获取用户id
        User user = (User) req.getSession().getAttribute("user");

        new CourseDao().add(course,user);

        resp.setContentType("text/html; charset=UTF-8");
        resp.getWriter().println("<h1 style='color:green'>添加成功! 5秒后返回课程页面</h1>");
        resp.getWriter().println("<meta http-equiv='refresh' content='5;URL=index'>");
    }
}
